﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuanLyKho.Server;
using System.Transactions;

namespace QuanLyKho.Server.BLL
{
    public class PhieuNhapThuocChiTietBLL
    {
        public PhieuNhapThuocChiTietBLL()
        {

        }

        public bool CreatePhieuNhapChiTiet(PhieuNhapThuoc phieuNhap, List<PhieuNhapThuocChiTiet> listChiTietAdd, List<PhieuNhapThuocChiTiet> listChiTietUpdate)
        {
            bool isSuccess = false;

            using (TransactionScope trans = new TransactionScope())
            {
                try
                {
                    using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                    {
                        TonKhoThuocBLL tonKhoBLL = new TonKhoThuocBLL();
                        // Trường hợp thêm mới phiếu nhập
                        foreach (PhieuNhapThuocChiTiet item in listChiTietAdd)
                        {
                            // Thực hiện thêm mới chi tiết
                            PhieuNhapThuocChiTiet chiTiet = new PhieuNhapThuocChiTiet();
                            chiTiet.IdPhieuNhap = phieuNhap.IdPhieuNhap;
                            chiTiet.MaThuoc = item.MaThuoc;
                            chiTiet.SoLuong = item.SoLuong;
                            chiTiet.GiaNhap = item.GiaNhap;
                            chiTiet.GiaXuat = item.GiaXuat;
                            chiTiet.HanDung = item.HanDung;
                            chiTiet.SoLo = item.SoLo;
                            chiTiet.SoLuongConLai = item.SoLuong;
                            entities.PhieuNhapThuocChiTiets.Add(chiTiet);
                            entities.SaveChanges();

                            // Thực hiện cộng số lượng vào kho thuốc
                            tonKhoBLL.CongTruSoLuongTonKhoThuoc(item.MaThuoc, item.SoLuong, true, entities);
                        }

                        // Tạo ra chi tiết phiếu nhập
                        foreach (PhieuNhapThuocChiTiet item in listChiTietUpdate)
                        {
                            // Trường hợp cập nhật
                            // Lấy chi tiết đang có trong CSDL
                            PhieuNhapThuocChiTiet chiTietOld = entities.PhieuNhapThuocChiTiets.FirstOrDefault(u => u.IdPhieuNhapChiTiet == item.IdPhieuNhapChiTiet);
                            if (chiTietOld != null)
                            {
                                // Thực hiện trừ số lượng trong kho thuốc
                                tonKhoBLL.CongTruSoLuongTonKhoThuoc(chiTietOld.MaThuoc, chiTietOld.SoLuong, false, entities);

                                // Thực hiện xóa chi tiết đã tồn tại trước đó trong CSDL
                                entities.PhieuNhapThuocChiTiets.Remove(chiTietOld);
                                entities.SaveChanges();

                                // Thực hiện thêm lại chi tiết
                                PhieuNhapThuocChiTiet chiTiet = new PhieuNhapThuocChiTiet();
                                chiTiet.IdPhieuNhap = phieuNhap.IdPhieuNhap;
                                chiTiet.MaThuoc = item.MaThuoc;
                                chiTiet.SoLuong = item.SoLuong;
                                chiTiet.GiaNhap = item.GiaNhap;
                                chiTiet.GiaXuat = item.GiaXuat;
                                chiTiet.HanDung = item.HanDung;
                                chiTiet.SoLo = item.SoLo;
                                chiTiet.SoLuongConLai = item.SoLuong;
                                entities.PhieuNhapThuocChiTiets.Add(chiTiet);
                                entities.SaveChanges();

                                // Thực hiện cộng số lượng vào kho thuốc
                                tonKhoBLL.CongTruSoLuongTonKhoThuoc(item.MaThuoc, item.SoLuong, true, entities);
                            }
                        }
                    }

                    trans.Complete();
                    isSuccess = true;
                }
                catch (Exception)
                {
                    isSuccess = false;
                }
            }

            return isSuccess;
        }

        public IList<PhieuNhapThuocChiTiet> RetrievePhieuNhapChiTiet(int idPhieuNhap)
        {
            IList<PhieuNhapThuocChiTiet> list;
            using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
            {
                list = entities.PhieuNhapThuocChiTiets.Include("PhieuNhapThuoc")
                                                    .Include("Dm_Thuoc")
                                                    .Include("Dm_Thuoc.Dm_HamLuong")
                                                    .Include("Dm_Thuoc.Dm_DonViTinh")
                                                    .Include("Dm_Thuoc.Dm_QuyCach")
                                                    .Where(u => u.IdPhieuNhap == idPhieuNhap).ToList();
            }
            return list;
        }

        public List<PhieuNhapThuocChiTiet> RetrievePhieuNhapChiTietById(int idPhieuNhapChiTiet)
        {
            List<PhieuNhapThuocChiTiet> list;
            using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
            {
                list = entities.PhieuNhapThuocChiTiets.Where(u => u.IdPhieuNhapChiTiet == idPhieuNhapChiTiet).ToList();
            }
            return list;
        }

        public bool DeletePhieuNhapChiTiet(int idPhieuNhapChiTiet)
        {
            bool isSuccess = false;

            TonKhoThuocBLL tonKhoBLL = new TonKhoThuocBLL();

            using (TransactionScope trans = new TransactionScope())
            {
                try
                {
                    using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                    {
                        PhieuNhapThuocChiTiet chiTiet = entities.PhieuNhapThuocChiTiets.Single(u => u.IdPhieuNhapChiTiet == idPhieuNhapChiTiet);
                        if (chiTiet != null)
                        {
                            // Thực hiện trừ số lượng trong kho thuốc
                            tonKhoBLL.CongTruSoLuongTonKhoThuoc(chiTiet.MaThuoc, chiTiet.SoLuong, false, entities);
                            entities.PhieuNhapThuocChiTiets.Remove(chiTiet);
                            entities.SaveChanges();
                        }
                    }

                    trans.Complete();
                    isSuccess = true;
                }
                catch (Exception)
                {
                    isSuccess = false;
                }
            }

            return isSuccess;
        }

        /// <summary>
        /// Thực hiện công trừ số lượng còn lại trong chi tiết phiếu nhập thuốc
        /// </summary>
        /// <param name="maThuoc"></param>
        /// <param name="soLuong"></param>
        /// <param name="laCong"></param>
        public void CongTruSoLuongConLai(int idPhieuNhapChiTiet, int soLuong, bool laCong, QuanLyKhoEntities entities)
        {
            PhieuNhapThuocChiTiet chiTiet = entities.PhieuNhapThuocChiTiets.SingleOrDefault(u => u.IdPhieuNhapChiTiet == idPhieuNhapChiTiet);
            if (laCong)
            {
                chiTiet.SoLuongConLai += soLuong;
            }
            else
            {
                chiTiet.SoLuongConLai -= soLuong;
            }

            entities.SaveChanges();
        }

        public List<PhieuNhapThuocChiTiet> RetrieveAllPhieuNhapChiTietConTrongKho()
        {
            List<PhieuNhapThuocChiTiet> list = new List<PhieuNhapThuocChiTiet>();
            using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
            {
                list = entities.PhieuNhapThuocChiTiets
                                            .Include("Dm_Thuoc")
                                            .Include("Dm_Thuoc.Dm_DonViTinh")
                                            .Include("Dm_Thuoc.Dm_HamLuong")
                                            .Include("Dm_Thuoc.Dm_QuyCach")
                                            .Include("PhieuNhapThuoc")
                                            .Include("PhieuNhapThuoc.Dm_NhaPhanPhoi")
                                            .Where(u=>u.SoLuongConLai > 0)
                                            .ToList();
            }
            return list;
        }

        public bool LaCoThayDoiSoLuong(int idPhieuNhapChiTiet, int soLuong, out PhieuNhapThuocChiTiet chiTiet)
        {
            bool laCoThayDoi = false;
            using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
            {
                chiTiet = entities.PhieuNhapThuocChiTiets.Single(u => u.IdPhieuNhapChiTiet == idPhieuNhapChiTiet);
                if (chiTiet.SoLuong != soLuong)
                {
                    laCoThayDoi = true;
                }
            }
            return laCoThayDoi;
        }

        public List<PhieuNhapThuocChiTiet> RetrieveAllPhieuNhapChiTietConTrongKho(string maThuoc, string tenThuoc)
        {
            List<PhieuNhapThuocChiTiet> list = new List<PhieuNhapThuocChiTiet>();
            using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
            {
                if (!string.IsNullOrEmpty(maThuoc))
                {
                    list = entities.PhieuNhapThuocChiTiets
                                            .Include("Dm_Thuoc")
                                            .Include("Dm_Thuoc.Dm_DonViTinh")
                                            .Include("Dm_Thuoc.Dm_HamLuong")
                                            .Include("Dm_Thuoc.Dm_QuyCach")
                                            .Include("PhieuNhapThuoc")
                                            .Include("PhieuNhapThuoc.Dm_NhaPhanPhoi")
                                            .Where(u => u.SoLuong > 0 && u.MaThuoc.StartsWith(maThuoc))
                                            .ToList();
                }

                if (!string.IsNullOrEmpty(tenThuoc))
                {
                    list = entities.PhieuNhapThuocChiTiets
                                           .Include("Dm_Thuoc")
                                           .Include("Dm_Thuoc.Dm_DonViTinh")
                                           .Include("Dm_Thuoc.Dm_HamLuong")
                                           .Include("Dm_Thuoc.Dm_QuyCach")
                                           .Include("PhieuNhapThuoc")
                                           .Include("PhieuNhapThuoc.Dm_NhaPhanPhoi")
                                           .Where(u => u.SoLuong > 0 && u.Dm_Thuoc.TenThuoc.StartsWith(tenThuoc))
                                           .ToList();
                }
            }
            return list;
        }

        public PhieuNhapThuocChiTiet RetrieveAllPhieuNhapChiTietById(int idPhieuNhapChiTiet)
        {
            PhieuNhapThuocChiTiet chiTiet = null;
            using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
            {
                chiTiet = entities.PhieuNhapThuocChiTiets
                                            .Include("Dm_Thuoc")
                                            .Include("Dm_Thuoc.Dm_DonViTinh")
                                            .Include("Dm_Thuoc.Dm_HamLuong")
                                            .Include("Dm_Thuoc.Dm_QuyCach")
                                            .Include("PhieuNhapThuoc")
                                            .Include("PhieuNhapThuoc.Dm_NhaPhanPhoi")
                                            .Single(u => u.IdPhieuNhapChiTiet == idPhieuNhapChiTiet);
            }
            return chiTiet;
        }
    }
}
